package com.example.canalclientsync.canal.handler;

import com.example.canalclientsync.canal.po.Project;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import top.javatool.canal.client.annotation.CanalTable;
//import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;

/**
 * 项目改名
 * 项目改名时，文件夹也会修改。然后其下的文件夹、模型数据均会更新 DirHandler
 *
 * @author chen.cr
 * @since 2023/9/19
 */
@Deprecated
//@Component
//@CanalTable("t_project")
@Slf4j
public class ProjectHandler implements EntryHandler<Project> {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public void insert(Project project) {
        log.info("insert project");
        log.info("nothing to do.");
    }

    @Override
    public void update(Project before, Project after) {
        log.info("update project");
//        Integer projectId = after.getId();
//        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("belongedProject.id", projectId);
//        NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("belongedProject", termQueryBuilder, ScoreMode.None);
//        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(nestedQueryBuilder);
//        SearchHits<EsModel> hits = elasticsearchRestTemplate.search(nativeSearchQuery, EsModel.class);
//        if (hits.hasSearchHits()) {
//            List<UpdateQuery> uqs = Lists.newArrayList();
//            for (SearchHit<EsModel> hit : hits.getSearchHits()) {
//                if (hit.getId() != null) {
//                    UpdateQuery updateQuery = UpdateQuery.builder(hit.getId())
//                                                         .withDocument(after.getDocument("belongedProject"))
//                                                         .build();
//                    uqs.add(updateQuery);
//                }
//            }
//            if (uqs.size() > 0) {
//                elasticsearchRestTemplate.bulkUpdate(uqs, EsModel.INDEX);
//                log.info("update EsModel project数据完成");
//            }
//        }
        log.info("nothing to do.");
    }

    @Override
    public void delete(Project project) {
        log.info("delete project");
        log.info("nothing to do.");
    }

}
